﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using TWS.Client;

public partial class admin_ChiTietToChuc : BaseAdminPage
{
    protected void btnOK_OnClick(object sender, EventArgs e)
    {
        //Page.Validate();
        //if (!Page.IsValid) return;
        //ToChucHelper spHP = new ToChucHelper();
        //ToChucDataSet spDS = new ToChucDataSet();
        //ToChucDataSet.ToChucRow ToChucmoi = spDS.ToChuc.NewToChucRow();
        //ToChucmoi.TenToChuc = txtTenToChuc.Text;
        //ToChucmoi.LoaiToChuc = Convert.ToInt32(ddlLoaiCon.SelectedValue);
        //ToChucmoi.HangSanXuat = txtHangSanXuat.Text;
        //ToChucmoi.ChungLoai = txtChungLoai.Text;
        //ToChucmoi.Gia = Convert.ToInt32(txtGia.Text);
        //////UploadImage();
        //if (txtHinhAnh.Text == string.Empty)
        //{
        //    txtHinhAnh.Text = "noimage.jpg";
        //}
        //ToChucmoi.HinhAnh = txtHinhAnh.Text;
        //ToChucmoi.ToChucMoi = ckToChucMoi.Checked;
        //ToChucmoi.ThuTu = Convert.ToInt32(txtThuTu.Text);
        //ToChucmoi.NgayDang = DateTime.Now;

        //ToChucmoi.TongQuan = txtTongQuan.Value;
        //ToChucmoi.ThongSoKyThuat = txtThongSo.Text;
        //ToChucmoi.Album = txtAlbum.Text;
        //spDS.ToChuc.AddToChucRow(ToChucmoi);
        //spHP.SaveToChuc(spDS);
        //RedirectPage(Request.RawUrl, true);
        SaveData();
    }

    private ToChucDataSet m_ToChucDataSet;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            LoadData();
            try
            {
                string mode = Request.QueryString["mode"];
                bool isNew = false;
                if (mode != "edit")
                {
                    isNew = true;
                }
                string key = Request.QueryString["id"];
                if (!isNew)
                {
                    if (key != null && key != string.Empty)
                    {
                        LoadData(key);
                    }
                }
                else
                {
                    m_ToChucDataSet = new ToChucDataSet();

                    ToChucDataSet.ToChucRow danhMuc = m_ToChucDataSet.ToChuc.NewToChucRow();
                    danhMuc.ID = -1;

                    m_ToChucDataSet.ToChuc.AddToChucRow(danhMuc);
                    Session[this.Request.Url.AbsolutePath.ToString() + "_" + "Data"] = m_ToChucDataSet;
                }

            }
            catch (Exception ex)
            {
                //DisplayMesseage(ex.Message);
                this.RedirectPage("pgError.aspx", false);
            }
        }
    }

    private void LoadData()
    {

        ddlLoaiBai1.DataSource = LoaiDataHelper.LoadLoaiToChuc();
        ddlLoaiBai1.DataTextField = "Value";
        ddlLoaiBai1.DataValueField = "Key";
        ddlLoaiBai1.DataBind();

        ddlLang1.DataSource = LoaiDataHelper.LoadNgonNgu();
        ddlLang1.DataTextField = "Value";
        ddlLang1.DataValueField = "Key";
        ddlLang1.DataBind();
    }


    private void LoadData(string benhLyId)
    {
        int itemId;
        if (int.TryParse(benhLyId, out itemId))
        {
            ToChucHelper helper = new ToChucHelper();
            ToChucDataSet tempDs = helper.LoadToChuc(itemId);


            ToChucDataSet.ToChucRow danhMuc = tempDs.ToChuc.FindByID(itemId);
            m_ToChucDataSet = new ToChucDataSet();
            m_ToChucDataSet.ToChuc.ImportRow(danhMuc);

            Session[this.Request.Url.AbsolutePath.ToString() + "_" + "Data"] = m_ToChucDataSet;

            txtTenSanPham.Text = danhMuc.Ten;
            lbTenSanPham.Text = danhMuc.Ten;
            if (!danhMuc.IsHinhAnhNull()) txtHinhAnh.Text = danhMuc.HinhAnh;
            string saveLocation = MyFilePathHelper.GetImageFolderURL(this);
            this.imageDemons.Src = saveLocation + txtHinhAnh.Text;

            if (!danhMuc.IsLangNull()) ddlLang1.Text = danhMuc.Lang.ToString();
            if (!danhMuc.IsLoaiNull()) ddlLoaiBai1.Text = danhMuc.Loai.ToString();
            if (!danhMuc.IsThuTuNull()) txtThuTu.Text = danhMuc.ThuTu.ToString();
            //}
        }
    }

    protected void btnSave_Click(object sender, EventArgs e)
    {

    }

    protected void btnCancel_Click(object sender, EventArgs e)
    {
        this.RedirectLastVisitedPage();
    }

    private void SaveData()
    {
        try
        {
            string mode = Request.QueryString["mode"];
            m_ToChucDataSet = (ToChucDataSet)Session[this.Request.Url.AbsolutePath.ToString() + "_" + "Data"];
            //SetViewMode(canEdit);
            ToChucDataSet.ToChucRow danhMuc = m_ToChucDataSet.ToChuc[0];

            danhMuc.Ten = txtTenSanPham.Text;
            danhMuc.ThuTu = Convert.ToInt32(txtThuTu.Text);
            danhMuc.Loai = Convert.ToInt32(ddlLoaiBai1.SelectedValue);
            danhMuc.Lang = Convert.ToInt32(ddlLang1.SelectedValue);

            UploadImage();
            if (txtHinhAnh.Text == string.Empty)
            {
                txtHinhAnh.Text = "noimage.jpg";
            }
            danhMuc.HinhAnh = txtHinhAnh.Text;

            ToChucHelper helper = new ToChucHelper();
            helper.SaveToChuc(m_ToChucDataSet);
        }
        catch (Exception ex)
        {
            //DisplayMesseage(ex.Message);
            this.RedirectPage("pgError.aspx", false);
        }
        this.RedirectPage("QuanLyToChuc.aspx", false);
    }

    private void UploadImage()
    {
        if (chkDeleteFile.Checked && txtHinhAnh.Text != string.Empty)
        {
            //delete old image
            try
            {
                string saveLocation = MyFilePathHelper.GetImageFolderURL_Saving(this);
                MyFilePathHelper.DeleteResource(saveLocation, txtHinhAnh.Text);
            }
            catch (Exception)
            {
            }
            finally
            {
                txtHinhAnh.Text = "noimage.jpg";
            }
            //postpone new image
        }
        //save new image
        if (this.inputFile.PostedFile != null && this.inputFile.PostedFile.ContentLength > 0)
        {
            string fn = System.IO.Path.GetFileName(inputFile.PostedFile.FileName);
            string path = MyFilePathHelper.GetImageFolderURL_Saving(this);
            fn = MyFilePathHelper.AppendTimeStamp(fn);
            string saveLocation = path + fn;

            inputFile.PostedFile.SaveAs(saveLocation);
            FileStream fs = File.Open(saveLocation, FileMode.Open, FileAccess.Read);
            byte[] inByte = new byte[fs.Length];
            fs.Read(inByte, 0, inByte.Length);
            fs.Close();
            MyFilePathHelper.DeleteResource(path, fn);
            ImagesConvertHelper iconvert = new ImagesConvertHelper();
            string filename;
            if (iconvert.ConvertImage(saveLocation, inByte, 6, out filename) == false)
                txtHinhAnh.Text = string.Empty;
            else
            {
                txtHinhAnh.Text = fn + ".JPG";
            }
        }
    }

}